from django.db import models
from django.contrib.auth.models import User, Group
from django.utils.timezone import now
from app.common import JudgeStatus, HomeWorkStatus
from ckeditor.fields import RichTextField


# Tag
class ProblemTag(models.Model):
    name = models.CharField(max_length=15, db_index=True)

    class Meta:
        db_table = "problem_tag"


# 习题
class Problem(models.Model):
    # 显示的 ID 即pk
    _id = models.CharField(max_length=15, db_index=True)
    # 标题
    title = models.CharField(max_length=200)
    # HTML 描述
    description = models.TextField()
    # 输入描述
    input_description = models.TextField()
    # 输出描述
    output_description = models.TextField()
    # 解决方案
    solution = models.TextField()
    # 样例输入输出，可以是多个 [{input: "test", output: "123"}, {input: "test123", output: "456"}]
    samples = models.TextField()
    # 测试数据文件名称
    test_case_id = models.CharField(max_length=20)
    # 提示
    hint = models.TextField()
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True)
    # ms
    time_limit = models.IntegerField()
    # MB
    memory_limit = models.IntegerField()
    # 是否可见
    visible = models.BooleanField()
    # 提交次数
    submission_number = models.IntegerField()
    # 通过次数
    accepted_number = models.IntegerField()
    # 难度
    difficulty = models.CharField(max_length=10)
    # 来源
    source = models.CharField(max_length=100)
    # TAG
    tags = models.ManyToManyField(ProblemTag)

    class Meta:
        db_table = "problem"
        ordering = ("_id",)

    def add_ac_number(self, ac=1):
        self.accepted_number = models.F("accepted_number") + ac
        self.submission_number = models.F("submission_number") + 1
        self.save(update_fields=["accepted_number", "submission_number"])


class Special(models.Model):
    title = models.CharField(max_length=50)
    description = models.TextField()
    problems_count = models.IntegerField()
    problems = models.ManyToManyField(Problem)
    order_num = models.IntegerField()
    group_name = models.CharField(max_length=20)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "special"


# 提交习题代码
class Submission(models.Model):
    problem = models.ForeignKey(Problem, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    code = models.TextField()
    result = models.IntegerField(db_index=True, default=JudgeStatus.PENDING)
    # 从JudgeServer返回的判题详情
    info = models.TextField()
    # 存储该提交所用时间和内存值，方便提交列表显示
    # {time_cost: "", memory_cost: "", err_info: ""}
    statistic_info = models.TextField()
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "submission"
        ordering = ("-create_time",)

    def __str__(self):
        return self.id


# 习题的评论
class Discuss(models.Model):
    problem = models.ForeignKey(Problem, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField()
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "discuss"
        ordering = ("-create_time",)


# 作业
class HomeWork(models.Model):
    title = models.CharField(max_length=50)
    description = models.TextField()
    special = models.ForeignKey(Special, on_delete=models.CASCADE, null=True)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    create_time = models.DateTimeField(auto_now_add=True)
    # 是否可见 false的话相当于删除
    visible = models.BooleanField()
    group = models.ForeignKey(Group, on_delete=models.CASCADE, null=True)

    @property
    def status(self):
        if self.start_time > now():
            # 没有开始 返回1
            return HomeWorkStatus.HOMEWORK_NOT_START
        elif self.end_time < now():
            # 已经结束 返回-1
            return HomeWorkStatus.HOMEWORK_ENDED
        else:
            # 正在进行 返回0
            return HomeWorkStatus.HOMEWORK_UNDERWAY

    class Meta:
        db_table = "homework"
        ordering = ("-start_time",)


# 系统设置选项
class SysOptions(models.Model):
    key = models.CharField(max_length=50, unique=True, db_index=True)
    value = models.TextField()


# blog Tag
class BlogTag(models.Model):
    name = models.CharField(max_length=15, db_index=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "blog_tag"


# blog category
class BlogCategory(models.Model):
    name = models.CharField(max_length=15, db_index=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "blog_category"


# blog
class Blog(models.Model):
    title = models.CharField(max_length=50)
    content = RichTextField(config_name='my_config')
    lead = RichTextField(config_name='my_config')
    create_time = models.DateTimeField(auto_now_add=True)
    tags = models.ManyToManyField(BlogTag)
    categories = models.ManyToManyField(BlogCategory)

    def __str__(self):
        return self.title

    class Meta:
        db_table = "blog"
